﻿
Imports System.Data.OleDb
Imports System.Data
Public Class Dabka_Detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     ' Hide adding comment, likes, adding tags, and rating from unregistred users
        If User.Identity.Name = "" Then
            Panel1.Visible = False
            Panel2.Visible = True
        End If

        ' set default value for title
        Dim dr As OleDbDataReader
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Try
            oleDbConn.Open()
            Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT Dabka_Name FROM Dabka Where ID=@F1", oleDbConn)
            TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
            dr = TagExistCmd.ExecuteReader()
            dr.Read()
            TB_Title.Text = "Re : " & dr.GetValue(0)
        Catch ex As Exception
            TB_Title.Text = ""
        End Try
        Try
            Dim drlike As OleDbDataReader
            Dim LikeCmd As OleDbCommand = New OleDbCommand("SELECT 'OK' FROM Dabka_Likes Where Dabka_fkID=@f1 and Username = @f2", oleDbConn)
            LikeCmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
            LikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            oleDbConn.Open()
            drlike = LikeCmd.ExecuteReader()
            drlike.Read()

            If drlike.GetValue(0) <> "" Then
                img_Like.Visible = True
                Btn_like.Visible = False
            End If
        Catch ex As Exception
            img_Like.Visible = False
            Btn_like.Visible = True
        End Try

        ' log user activity - who viewed this Dabke... taken from https://eric.stca.herts.ac.uk/comqsjb/webscripting/
        If User.Identity.Name <> "" Then
            Try
                Dim ViewSQL As String = "INSERT INTO DabkaViewed(Dabka_fkID, UserName, view_date) VALUES(@f1,@f2,Now())"
                Dim ViewCommand As OleDbCommand = New OleDbCommand(ViewSQL, oleDbConn)
                ViewCommand.CommandType = CommandType.Text
                ViewCommand.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                ViewCommand.Parameters.AddWithValue("@f2", User.Identity.Name)
                ViewCommand.ExecuteNonQuery()
            Catch ex As Exception
                ' probably there is duplicates, just ignore it.
            End Try
        End If


    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
  
        ' add the comments


        If Request.QueryString("ID") <> "" Then
            Dim sql As String = "INSERT INTO Dabka_Comments (C_Title, C_Text, C_Writer, C_Date, Dabka_fkID) VALUES (@f1,@f2,@f3,NOW(),@f5)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", TB_Title.Text)
            cmd.Parameters.AddWithValue("@f2", TB_Text.Text)
            cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("ID"))
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            Response.Redirect("Dabka_Detail.aspx?ID=" & Request.QueryString("ID"))
        End If
    End Sub

    Protected Sub Tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Tag.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO DabkaTags(Dabka_fkID, Tags_fkID) VALUES (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If TextBox1.Text = "" Then Return

        Dim newTags = TextBox1.Text.Split(",")

        For Each newTag In newTags

            Dim TagExistId As Double
            Dim dr As OleDbDataReader

            Try

                Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT Tag_ID FROM Tags Where Tag_Name=@F1", oleDbConn)
                TagExistCmd.Parameters.AddWithValue("@f1", newTag)
                dr = TagExistCmd.ExecuteReader()
                dr.Read()
                TagExistId = dr.GetValue(0)
            Catch ex As Exception
                TagExistId = 0
            End Try



            If TagExistId <= 0 Then
                ' insert that tag
                Dim newTagsql As String = "INSERT INTO Tags(Tag_Name) VALUES(@f1)"
                Dim NewTagcommand As OleDbCommand = New OleDbCommand(newTagsql, oleDbConn)
                NewTagcommand.CommandType = CommandType.Text
                NewTagcommand.Parameters.AddWithValue("@f1", newTag)
                NewTagcommand.ExecuteNonQuery()

                ' get the ID of last inserted tag

                Dim getlaslIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 Tag_ID FROM Tags ORDER BY Tag_ID DESC", oleDbConn)
                Dim lastIdDR = getlaslIdCmd.ExecuteReader()
                lastIdDR.Read()
                TagExistId = lastIdDR.GetValue(0)
            End If

            'add it to the Dabka

            Dim TopicTagcmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
            TopicTagcmd.CommandType = CommandType.Text
            TopicTagcmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
            TopicTagcmd.Parameters.AddWithValue("@f2", TagExistId)
            TopicTagcmd.ExecuteNonQuery()
        Next
        Response.Redirect("SearchByTag.aspx")

    End Sub


    Protected Sub btn_Rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Rate.Click
        ' if rating already exist then delete it
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim deleteRatingSql As String = "DELETE FROM Dabka_Rating WHERE Dabka_fkID=@f1 AND Username=@f2"
        Dim deleteRatingCommand As New OleDbCommand(deleteRatingSql, oleDbConn)
        deleteRatingCommand.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        deleteRatingCommand.Parameters.AddWithValue("@f2", User.Identity.Name)
        deleteRatingCommand.ExecuteNonQuery()

        ' Insert the rating
        Dim InsertRatingSql As String = "INSERT INTO Dabka_Rating (Dabka_fkID,Username,Rate) VALUES (@f1,@f2,@f3)"
        Dim InsertRatingCommand As New OleDbCommand(InsertRatingSql, oleDbConn)
        InsertRatingCommand.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        InsertRatingCommand.Parameters.AddWithValue("@f2", User.Identity.Name)
        InsertRatingCommand.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        InsertRatingCommand.ExecuteNonQuery()
        lbl_Rating.Text = "Thank you for rating !!"
    End Sub

    Protected Sub Btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_like.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim LikeCmd As OleDbCommand = New OleDbCommand("Insert into Dabka_Likes(Dabka_fkID, UserName, Liketime) values (@f1, @f2,Now()) ", oleDbConn)
        LikeCmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        LikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        LikeCmd.ExecuteNonQuery()

        img_Like.Visible = True
        Btn_like.Visible = False

        oleDbConn.Close()
        LikeCmd.Dispose()
        oleDbConn.Dispose()
    End Sub

    Private Function oleDbConn() As Object
        Throw New NotImplementedException
    End Function

End Class